# Copyright (c) 2016, Ruslan Baratov
# All rights reserved.

cmake_minimum_required(VERSION 3.0)
project(Hunter)

include(ExternalProject) # ExternalProject_Add

include("@HUNTER_SELF@/cmake/Hunter")

# Scheme for download and install OpenSSL library for Windows

list(APPEND CMAKE_MODULE_PATH "@HUNTER_SELF@/cmake/modules")

include(hunter_fatal_error)
include(hunter_internal_error)
include(hunter_status_debug)
include(hunter_assert_not_empty_string)

hunter_status_debug("Scheme: url_sha1_openssl_windows_1_1_plus")

# Check preconditions
hunter_assert_not_empty_string("@HUNTER_SELF@")
hunter_assert_not_empty_string("@HUNTER_EP_NAME@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_URL@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SHA1@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_DOWNLOAD_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_SOURCE_DIR@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_INSTALL_PREFIX@")
hunter_assert_not_empty_string("@HUNTER_MSVC_ARCH@")
hunter_assert_not_empty_string("@HUNTER_MSVC_ARCH_TARGET@")
hunter_assert_not_empty_string("@HUNTER_MSVC_VCVARSALL@")
hunter_assert_not_empty_string("@HUNTER_PACKAGE_LICENSE_DIR@")
hunter_assert_not_empty_string("@HUNTER_GLOBAL_SCRIPT_DIR@")
hunter_assert_not_empty_string("@HUNTER_TLS_VERIFY@")

# `find_package(Perl)` is not suitable because `perl` from
# cygwin directory can be found
execute_process(
    COMMAND
    "perl"
    "--version"
    RESULT_VARIABLE
    PERL_VERSION
)

if(NOT "${PERL_VERSION}" EQUAL 0)
  message("Current PATH directories:")
  message(">>>")
  foreach(x $ENV{PATH})
    foreach(y ${x})
      message("    ${y}")
    endforeach()
  endforeach()
  message("\n<<<")

  hunter_fatal_error(
      "OpenSSL: perl not found"
      ERROR_PAGE "error.openssl.perl.not.found"
  )
endif()

string(COMPARE EQUAL "@HUNTER_MSVC_ARCH_TARGET@" "amd64" is_amd64)
string(COMPARE EQUAL "@HUNTER_MSVC_ARCH_TARGET@" "arm64" is_arm64)
if(is_amd64 OR is_arm64)
  set(arch "VC-WIN64A")
else()
  set(arch "VC-WIN32")
endif()

if(DEBUG_RUNTIME)
  set(arch "debug-${arch}")
endif()

if(ASM_SUPPORT)
  hunter_add_package(NASM) # set NASM_ROOT
  set(opt "")
else()
  set(NASM_ROOT "")
  set(opt "no-asm")
endif()

if("@MSVC@")
  # Logging as Workaround for VS_UNICODE_OUTPUT issue:
  # https://public.kitware.com/Bug/view.php?id=14266
  set(log_opts LOG_CONFIGURE 1)
else()
  set(log_opts "")
endif()

if (BUILD_SHARED_LIBS)
  set(shared "shared")
else()
  set(shared "no-shared")
endif()

set(openssl_dir "@HUNTER_PACKAGE_INSTALL_PREFIX@/ssl")
set(openssl_install_dir "@HUNTER_PACKAGE_INSTALL_PREFIX@")

# Used variables:
#   * openssl_dir
#   * openssl_install_dir
#   * arch
#   * opt
#   * shared
#   * NASM_ROOT
configure_file(
    "@HUNTER_PACKAGE_SETUP_DIR@/ep-stages/configure_1_1_plus.cmake.in"
    "@HUNTER_PACKAGE_BUILD_DIR@/configure_1_1_plus.cmake"
    @ONLY)

file(MAKE_DIRECTORY "${openssl_dir}")

ExternalProject_Add(
    "@HUNTER_EP_NAME@"
    URL
    "@HUNTER_PACKAGE_URL@"
    URL_HASH
    SHA1=@HUNTER_PACKAGE_SHA1@
    DOWNLOAD_DIR
    "@HUNTER_PACKAGE_DOWNLOAD_DIR@"
    TLS_VERIFY
    "@HUNTER_TLS_VERIFY@"
    SOURCE_DIR
    "@HUNTER_PACKAGE_SOURCE_DIR@"
    INSTALL_DIR
    "@HUNTER_PACKAGE_INSTALL_PREFIX@"
        # not used, just avoid creating Install/<name> empty directory
    CONFIGURE_COMMAND
    "@HUNTER_MSVC_VCVARSALL@" "@HUNTER_MSVC_ARCH@"
    COMMAND
    "@CMAKE_COMMAND@" -P "@HUNTER_PACKAGE_BUILD_DIR@/configure_1_1_plus.cmake"
    COMMAND
    nmake install
    BUILD_COMMAND
    "@CMAKE_COMMAND@" -E echo "Dummy build step"
    BUILD_IN_SOURCE
    1
    INSTALL_COMMAND
    "@CMAKE_COMMAND@"
    "-C@HUNTER_ARGS_FILE@" # for 'HUNTER_INSTALL_LICENSE_FILES'
    "-Dsrcdir=@HUNTER_PACKAGE_SOURCE_DIR@"
    "-Ddstdir=@HUNTER_PACKAGE_LICENSE_DIR@"
    -P
    "@HUNTER_SELF@/scripts/try-copy-license.cmake"
    ${log_opts}
)
